package net.pimpas.security.jaas.serviceprovider.auth.login;

import javax.security.auth.Subject;
import javax.security.auth.login.LoginException;

import net.pimpas.security.jaas.serviceprovider.auth.JAASSubject;
import net.pimpas.security.jaas.serviceprovider.auth.login.callback.JAASCallbackHandler;
import net.pimpas.security.serviceprovider.auth.login.LoginContext;

public class JAASLoginContext implements LoginContext {
	
	private javax.security.auth.login.LoginContext loginContext;
	
	public JAASLoginContext(String name) throws LoginException {
		this.setLoginContext(new javax.security.auth.login.LoginContext(name));
	}
	
	public JAASLoginContext(String name, JAASSubject subject, JAASCallbackHandler callbackhandler) throws LoginException {
		this.setLoginContext(new javax.security.auth.login.LoginContext(name, subject.getSubject(), callbackhandler));
	}
	
	
	public JAASLoginContext(String name, JAASCallbackHandler callbackhandler) throws LoginException {
		this.setLoginContext(new javax.security.auth.login.LoginContext(name, callbackhandler));
	}

	public javax.security.auth.login.LoginContext getLoginContext() {
		return loginContext;
	}

	public void setLoginContext(javax.security.auth.login.LoginContext loginContext) {
		this.loginContext = loginContext;
	}

	@Override
	public boolean equals(Object obj) {
		return loginContext.equals(obj);
	}

	public Subject getSubject() {
		return loginContext.getSubject();
	}

	@Override
	public int hashCode() {
		return loginContext.hashCode();
	}

	public void login()
			throws net.pimpas.security.serviceprovider.auth.login.LoginException {
		try {
			loginContext.login();
		} catch (LoginException e) {
			throw (net.pimpas.security.serviceprovider.auth.login.LoginException)e.getCause();
		}
	}

	public void logout()
			throws net.pimpas.security.serviceprovider.auth.login.LoginException {
		try {
			loginContext.logout();
		} catch(LoginException e) {
			throw (net.pimpas.security.serviceprovider.auth.login.LoginException)e.getCause();
		}
	}

	@Override
	public String toString() {
		return loginContext.toString();
	}
}